

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';
import 'package:go_router/go_router.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shop/pages/web_view_page.dart';
import 'package:shop/route/routes.dart';
import 'package:shop/utils/token_utils.dart';
import 'package:provider/provider.dart';  // 注意是小写的 "provider"
import '../../datas/homeListData.dart';
import '../../utils/md5.dart';
import '../login/api.dart';

class HomePage extends StatefulWidget{
  const HomePage({super.key});
  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage>{
LoginViewModel viewModel = LoginViewModel();
RefreshController refreshController = RefreshController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
     // initListItemData();
    Map<String,dynamic> loginData = {};
    loginData["mobile"] = "19939962786";
    loginData["password"] = MD5Utils.encrypt("123");
    viewModel.login(loginData);
  }
// void initListItemData()async{
//   Map<String,dynamic> loginData = {};
//   loginData["mobile"] = "19939962786";
//   loginData["password"] = MD5Utils.encrypt("123");
//   final HomeListItemData? userList =await LoginViewModel.login(loginData);
//
//   setState(() {
//     _list = userList?.corpList;
//   });
//
// }
  @override
  Widget build(BuildContext context) {

    Widget _listItemVIew(CorpList data){

      return GestureDetector(onTap: (){
        String title = "首页过去的1";
        context.go("/web_view_page/$title");
      },child:  Container(

        margin:EdgeInsets.only(
            top: 5.h,bottom: 5.h,left: 10.w,right: 10.w
        ) ,
        padding:EdgeInsets.only(
            top: 15.h,bottom: 15.h,left: 10.w,right: 10.w
        ),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(5.r)),
            border: Border.all(
                color:Colors.grey,
                width: 0.5.r
            )
        ),
        child: Column(

          children: [
            Row(

              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(30.r),
                  child:  Image.network("https://jx-file-space.oss-cn-beijing.aliyuncs.com/988069/20250815/8c30227ab7cc1eec477d1aa981a207d2.png",
                    width: 30.r,
                    height: 30.r,

                  ),
                ),
                SizedBox(width: 5.w,),
                Padding(padding:EdgeInsets.only(left: 5.w),child: Text("${data.corpName}",style: TextStyle(color: Colors.black,fontSize:15.sp ),),),
                Expanded(child: SizedBox()),
                Padding(padding:EdgeInsets.only(right: 5.w),child: Text("2025-10-28",style: TextStyle(color: Colors.black),),),
                Text("置顶",style: TextStyle(
                    fontWeight: FontWeight.w600
                ),)
              ],
            ),
            Container(
              margin: EdgeInsets.only(top: 5.h),
              width: double.infinity,
              child: Text("${data.corpCode}",textAlign: TextAlign.left,),
            ),
            Row(
              children: [
                Text("分类",style: TextStyle(color: Colors.green),),
                Expanded(child: SizedBox()),
                Image.asset("assets/images/icon/about-us.png",width: 30.r,height: 30.r,)
              ],
            )
          ],
        ),
      ),);

    }

    Widget _list(){
      return  Consumer<LoginViewModel>(builder: (context,value,child){
        return  ListView.builder(
          // shrinkWrap: true 时：ListView 的高度会 “收缩” 到刚好能包裹所有子项的总高度（即内容有多长，ListView 就有多高）。此时 ListView 的高度由自身内容决定，而非父容器。
          shrinkWrap: true,//
          physics: const NeverScrollableScrollPhysics(),
          itemBuilder: (context,index){
            return _listItemVIew(value.list![index]);
          },
          itemCount: value.list?.length??0,
        );
      }

      );
    }

    Widget _banner(){
        return        SizedBox(
          width: double.infinity,
          height: 150.h,
          child: Swiper(

            itemBuilder: (context,index){
              return Container(height: 150.h,color: Colors.lightBlue,
                width: double.infinity,
                child: Image.network("https://jx-file-space.oss-cn-beijing.aliyuncs.com/988069/20250815/8c30227ab7cc1eec477d1aa981a207d2.png",fit: BoxFit.fill,),

              );
            },
            itemCount:3,
            pagination: const SwiperPagination(),
            control: const SwiperControl(),
          ) ,
        );
    }
    return ChangeNotifierProvider(create: (context){
      return viewModel;
    },
    child:  Scaffold(
      floatingActionButton: FloatingActionButton(onPressed: (){
        //推出登陆
        TokenUtils.clearToken();
        AppRouter().router.push("/login");
      },
        child: Text("退出"),

      ),
      body: SafeArea(child:
          SmartRefresher(controller:refreshController,
          onRefresh: (){
            //下拉刷新
            refreshController.refreshCompleted();
          },
            onLoading: (){
            //上拉加载
              refreshController.loadComplete();
            },
            
          child:   SingleChildScrollView(
            child:  Column(
                children: [
                  _banner(),
                  _list()

                ]
            ),
          ),
          )

      ),
    ),
    )

     ;
  }
}